<?php

namespace Admins\Controller;

use Common\Controller\AdminBase;

class AutomaticController extends AdminBase
{
    public function index()
    {
        if (IS_POST) {
            $this->redirect('index', $_POST);
        }
        $get = I('get.');
        if(!empty($get['type']))
        {
            $type = $get['type'];
            if($type == 1)
            {
                $table = "Works";
                $where['status'] = 1;
                $where['type'] = 1;
            }
            elseif($type == 2) 
            {
                $table = "Suipai";
                $where['status'] = 1;
            }
            elseif($type == 3)
            {
                $table = "Works";
                $where['status'] = 1;
                $where['type'] = 2;
            }
            elseif($type == 4)
            {
                $table = "News";
                $where['status'] = 1;
            }
            elseif($type == 5)
            {
                $table = "Activity";
                $where['status'] = 1;
            }
            elseif($type == 6)
            {
                $table = "Teachlist";
                $where['status'] = 1;
            }
            $keyword = \Input::getVar(I('get.keyword'));
            if (!empty($keyword)) {
                $this->assign("keyword", $keyword);
                $where['title'] = array("LIKE", "%{$keyword}%");
            }
        }
        else 
        {
            $table = "Works";
            $type = 1;
            $where['type'] = 1;
            $where['status'] = 1;
        }
        $this->assign("type", $type);
        
        $count = M($table)->where($where)->count();
        $page = $this->page($count, 20);
        $list = M($table)->where($where)->limit($page->firstRow . ',' . $page->listRows)->order('create_time desc')->select();
        
        if($list)
        {
            foreach($list as $k => $v)
            {
                if($type == 1)
                {
                    $list[$k]['url'] = "/works/".$v['workid'];
                    $list[$k]['temp_id'] = $v['workid'];
                }
                elseif($type == 2)
                {
                    $list[$k]['url'] = "/suipai/".$v['id'];
                    $list[$k]['temp_id'] = $v['id'];
                }
                elseif($type == 3)
                {
                    $list[$k]['url'] = "/copy/".$v['workid'];
                    $list[$k]['temp_id'] = $v['workid'];
                }
                elseif($type == 4)
                {
                    $list[$k]['url'] = "/news/".$v['id'].".html";
                    $list[$k]['temp_id'] = $v['id'];
                }
                elseif($type == 5)
                {
                    $list[$k]['url'] = "/activity/".$v['id'].".html";
                    $list[$k]['temp_id'] = $v['id'];
                }
                elseif($type == 6)
                {
                    $list[$k]['url'] = "/teach/".$v['teachid'];
                    $list[$k]['temp_id'] = $v['teachid'];
                }
            }
        }
        $this->assign("list", $list);
        $this->assign("Page", $page->show());
        
        $this->display();
    }
    
    public function addcomment()
    {
        if(IS_POST)
        {
            $post = I('post.');
            $type = $post['type'];
            $id = $post['id'];
            $num = $post['num'];
            if($num < 0)
            {
                $this->error("输入的数量不能小于0");
            }
            if($type == 1 || $type == 3)
            {
                $table = "Works";
                $where['workid'] = $id;
                $data['clicknum'] = array('exp', 'clicknum+'.$num);
            }
            elseif($type == 2)
            {
                $table = "Suipai";
                $where['id'] = $id;
                $data['clicknum'] = array('exp', 'clicknum+'.$num);
            }
            elseif($type == 4)
            {
                $table = "News";
                $where['id'] = $id;
                $data['clicknum'] = array('exp', 'clicknum+'.$num);
            }
            elseif($type == 5)
            {
                $table = "Activity";
                $where['id'] = $id;
                $data['clicknum'] = array('exp', 'clicknum+'.$num);
            }
            elseif($type == 6)
            {
                $table = "Teachlist";
                $where['teachid'] = $id;
                $data['viewnum'] = array('exp', 'viewnum+'.$num);
            }
            $check = M($table)->where($where)->save($data);
            if($check)
            {
                $this->success("添加成功", U('Automatic/index?type='.$type));
            }
            else 
            {
                $this->error("添加失败");
            }
        }
        else 
        {
            $get = I('get.');
            $type = $get['type'];
            $id = $get['id'];
            if($type == 1 || $type == 3)
            {
                $table = "Works";
                $where['workid'] = $id;
            }
            elseif($type == 2)
            {
                $table = "Suipai";
                $where['id'] = $id;
            }
            elseif($type == 4)
            {
                $table = "News";
                $where['id'] = $id;
            }
            elseif($type == 5) 
            {
                $table = "Activity";
                $where['id'] = $id; 
            }
            elseif($type == 6)
            {
                $table = "Teachlist";
                $where['teachid'] = $id;
            }
            $title = M($table)->where($where)->getField("title");
            $this->assign("type", $type);
            $this->assign("id", $id);
            $this->assign("title", $title);
            $this->display();
        }
    }
    
    public function addauto($data = array())
    {
        $type = I('get.type');
        $id = I('get.id');
        $user = array();
        if($data)
        {
            foreach($data as $v)
            {
                $userinfo = M('User')->where(array('uname' => $v['A']))->find();
                if($userinfo)
                {
                    $user[] = $userinfo;
                }
            }
        }
        $this->assign("user", $user);
        
        if($type == 1 || $type == 3)
        {
            $table = "Works";
            $where['workid'] = $id;
            if($type == 1)
            {
                $temp_status['comment'] = 1;
                $temp_status['dig'] = 1;
                $temp_status['fav'] = 1;
                $temp_status['fans'] = 1;
            }
            if($type == 3)
            {
                $temp_status['comment'] = 1;
                $temp_status['dig'] = 1;
                $temp_status['fans'] = 1;
            }
        }
        elseif($type == 2)
        {
            $table = "Suipai";
            $where['id'] = $id;
            $temp_status['comment'] = 1;
            $temp_status['fans'] = 1;
        }
        elseif($type == 4)
        {
            $table = "News";
            $where['id'] = $id;
            $temp_status['comment'] = 1;
        }
        elseif($type == 5)
        {
            $table = "Activity";
            $where['id'] = $id;
            $temp_status['comment'] = 1;
        }
        elseif($type == 6)
        {
            $table = "Teachlist";
            $where['teachid'] = $id;
            $temp_status['comment'] = 1;
            $temp_status['fav'] = 1;
            $temp_status['fans'] = 1;
        }
        
        $year = "<option value=\"".date('Y', time())."\">".date('Y', time())."年</option>";
//         for($i = 2015; $i <= 2020; $i++)
//         {
//             $select = "";
//             if($i == date('Y', time()))
//             {
//                 $select = "selected";
//             }
//             $year .= "<option value=\"$i\" $select>".$i."年</option>";
//         }
        $this->assign("year", $year);
        
        $month = "";
        for($i = 1; $i < 13; $i++)
        {
            $select = "";
            if($i == intval(date('m', time())))
            {
                $select = "selected";
            }
            $month .= "<option value=\"$i\" $select>".$i."月</option>";
        }
        $this->assign("month", $month);
        
        $day = "";
        for($i = 1; $i < 32; $i++)
        {
            $select = "";
            if($i == intval(date('d', time())))
            {
                $select = "selected";
            }
            $day .= "<option value=\"$i\" $select>".$i."日</option>";
        }
        $this->assign("day", $day);
        
        $hour = "";
        for($i = 1; $i < 25; $i++)
        {
            $select = "";
            if($i == intval(date('H', time())))
            {
                $select = "selected";
            }
            $hour .= "<option value=\"$i\" $select>".$i."时</option>";
        }
        $this->assign("hour", $hour);

        $minute = "";
        for($i = 0; $i < 60; $i++)
        {
            $select = "";
            if($i == intval(date('i', time())))
            {
                $select = "selected";
            }
                $minute .= "<option value=\"$i\" $select>".$i."分</option>";
        }
        $this->assign("minute", $minute);
        
        $title = M($table)->where($where)->getField("title");
        
        $this->assign("type", $type);
        $this->assign("id", $id);
        $this->assign("title", $title);
        $this->assign("temp_status", $temp_status);
        $this->display('addauto');
    }
    
    public function upload()
    {
        header("Content-Type:text/html;charset=utf-8");
        $upload = new \Think\Upload();// 实例化上传类
        $upload->rootPath = './f/user';
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('xls', 'xlsx');// 设置附件上传类
        $upload->savePath  =      '/'; // 设置附件上传目录
        // 上传文件
        $info   =   $upload->uploadOne($_FILES['file']);
        $filename = './f/user'.$info['savepath'].$info['savename'];
        $exts = $info['ext'];
        
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功
            $this->data_import($filename, $exts, $_GET['type']);
        }
    }
    
    protected function data_import($filename, $exts='xls', $type)
    {
        //导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
       // import("web.Libs.Util.PHPExcel");
        //创建PHPExcel对象，注意，不能少了\
        require(PROJECT_PATH."Libs/Util/PHPExcel.class.php");
        $PHPExcel=new \PHPExcel();
        //如果excel文件后缀名为.xls，导入这个类
        if($exts == 'xls'){
            //import("web.Libs.Util.PHPExcel.Reader.Excel5");
            require(PROJECT_PATH."Libs/Util/PHPExcel/Reader/Excel5.php");
            $PHPReader=new \PHPExcel_Reader_Excel5();
        }else if($exts == 'xlsx'){
            //import("web.Libs.Util.PHPExcel.Reader.Excel2007");
            require(PROJECT_PATH."Libs/Util/PHPExcel/Reader/Excel2007.php");
            $PHPReader=new \PHPExcel_Reader_Excel2007();
        }
        //载入文件
        $PHPExcel=$PHPReader->load($filename);
        //获取表中的第一个工作表，如果要获取第二个，把0改为1，依次类推
        $currentSheet=$PHPExcel->getSheet(0);
        //获取总列数
        $allColumn=$currentSheet->getHighestColumn();
        //获取总行数
        $allRow=$currentSheet->getHighestRow();
        //循环获取表中的数据，$currentRow表示当前行，从哪行开始读取数据，索引值从0开始
        $data = array();
        for($currentRow=2;$currentRow<=$allRow;$currentRow++){
            //从哪列开始，A表示第一列
            for($currentColumn='A';$currentColumn<=$allColumn;$currentColumn++){
                //数据坐标
                $address=$currentColumn.$currentRow;
                //读取到的数据，保存到数组$arr中
                $cell =$currentSheet->getCell($address)->getValue();
                //$cell = $data[$currentRow][$currentColumn];
                if($cell instanceof PHPExcel_RichText){
                    $cell  = $cell->__toString();
                }
                $data[$currentRow-2][$currentColumn] = $cell;
               // print_r($cell);echo "<br>";
            }
    
        }
        unlink($filename);
        if($type == 7)
        {
           $this->addsign($data);
        }
        else 
        {
           $this->addauto($data);
        }
        //$this->save_import($data);
        return $data;
    }
    
    public function addmsg()
    {
        $post = I('post.');
        if(!$post['userid'])
        {
            $this->error("请导入用户");
        }
        //print_r($post);
        $temp_time = strtotime($post['year']."-".$post['month']."-".$post['day']." ".$post['hour'].":".$post['minute']);
        $settime = $post['time'] * 60;
        $start = 0;
        foreach($post['userid'] as $k => $v)
        {
            $data['type'] = $post['type'];
            $data['infoid'] = $post['id'];
            $userid = $data['userid'] = $post['userid'][$k];
            $data['dig'] = empty($post['dig_'.$userid]) ? 0 : $post['dig_'.$userid];
            $data['fav'] = empty($post['fav_'.$userid]) ? 0 : $post['fav_'.$userid];
            $data['fans'] = empty($post['fans_'.$userid]) ? 0 : $post['fans_'.$userid];
            $data['comment'] = empty($post['comment_'.$userid]) ? 0 : $post['comment_'.$userid];
            $data['message'] = $post['message'][$k];
            if($data['comment'] == 1 && empty($data['message']))
            {
                $this->error("请补充留言信息");
                exit;
            }
            if($start != 0)
            {
                //$temp_time += rand(0, $settime);
                $temp_time += $settime;
            }
            $data['create_time'] = $temp_time;
          //  $data['time'] = date('Y-m-d H:i',$temp_time);
            $start = 1;
            $count = M('Auto')->where(array('type' => $data['type'], 'infoid' => $data['infoid'], 'userid' => $userid))->count();
            if($count > 0)
            {
                unset($data['userid']);
                M('Auto')->where(array('type' => $data['type'], 'infoid' => $data['infoid'], 'userid' => $userid))->save($data);
            }
            else 
            {
                M('Auto')->add($data);
            }
        }
        $this->success("成功添加", U("Automatic/index"));
    }
    
    public function sign()
    {
        $this->display();
    }
    
    public function addsign($data = array())
    {
        $type = 7;
        $this->assign("type", $type);
        $this->assign("id", 1);
        $user = array();
        if($data)
        {
            foreach($data as $v)
            {
                $userinfo = M('User')->where(array('uname' => $v['A']))->find();
                if($userinfo)
                {
                    $user[] = $userinfo;
                }
            }
        }
        $this->assign("user", $user);
        
        $year = "<option value=\"".date('Y', time())."\">".date('Y', time())."年</option>";
        //         for($i = 2015; $i <= 2020; $i++)
            //         {
            //             $select = "";
            //             if($i == date('Y', time()))
                //             {
                //                 $select = "selected";
                //             }
                //             $year .= "<option value=\"$i\" $select>".$i."年</option>";
                //         }
        $this->assign("year", $year);

        $month = "";
        for($i = 1; $i < 13; $i++)
        {
            $select = "";
            if($i == intval(date('m', time())))
            {
                $select = "selected";
            }
            $month .= "<option value=\"$i\" $select>".$i."月</option>";
        }
        $this->assign("month", $month);

        $day = "";
        for($i = 1; $i < 32; $i++)
        {
            $select = "";
            if($i == intval(date('d', time())))
            {
                $select = "selected";
            }
            $day .= "<option value=\"$i\" $select>".$i."日</option>";
        }
        $this->assign("day", $day);

        $hour = "";
        for($i = 1; $i < 25; $i++)
        {
            $select = "";
            if($i == intval(date('H', time())))
            {
                $select = "selected";
            }
            $hour .= "<option value=\"$i\" $select>".$i."时</option>";
        }
        $this->assign("hour", $hour);

        $minute = "";
        for($i = 0; $i < 60; $i++)
        {
            $select = "";
            if($i == intval(date('i', time())))
            {
                $select = "selected";
            }
            $minute .= "<option value=\"$i\" $select>".$i."分</option>";
        }
        $this->assign("minute", $minute);
        
        
        $this->display("addsign");
    }
    
    public function addsignmsg()
    {
        $post = I('post.');
        if(!$post['userid'])
        {
            $this->error("请导入用户");
        }
        //print_r($post);
        $temp_time = strtotime($post['year']."-".$post['month']."-".$post['day']." ".$post['hour'].":".$post['minute']);
        $settime = $post['time'] * 60;
        $start = 0;
        foreach($post['userid'] as $k => $v)
        {
            $data['type'] = $post['type'];
            $data['infoid'] = $post['id'];
            $userid = $data['userid'] = $post['userid'][$k];
            $data['dig'] = 0;
            $data['fav'] = 0;
            $data['fans'] = 0;
            $data['comment'] = 0;
            $data['message'] = "";
            if($start != 0)
            {
                $temp_time += rand(0, $settime);
            }
            $data['create_time'] = $temp_time;
            //  $data['time'] = date('Y-m-d H:i',$temp_time);
            $start = 1;
            $count = M('Auto')->where(array('type' => $data['type'], 'infoid' => $data['infoid'], 'userid' => $userid))->count();
            if($count > 0)
            {
                unset($data['userid']);
                M('Auto')->where(array('type' => $data['type'], 'infoid' => $data['infoid'], 'userid' => $userid))->save($data);
            }
            else
            {
                M('Auto')->add($data);
            }
        }
        $this->success("成功添加", U("Automatic/addsign"));
    }
    
}